<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 课堂逻辑
 */
class Classroom extends AdminBase
{
    

    
    /**
     * 获取课堂列表
     */
    public function getArticleList($where = [], $field = 'a.*,m.nickname', $order = '')
    {
        
        $this->modelCourse->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],

                ];

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $this->modelCourse->join = $join;

        return $this->modelCourse->getList($where, $field, $order);
    }
    
    /**
     * 获取课堂列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name|a.describe'] = ['like', '%'.$data['search_data'].'%'];
        !empty($data['teacher_id']) && $where['a.teacher_id'] =$data['teacher_id'];
        return $where;
    }
    
    /**
     * 课堂信息编辑
     */
    public function articleEdit($data = [])
    {

        $validate_result = $this->validateCourse->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateCourse->getError()];
        }
        
        $url = url('articleList');
        
        empty($data['id']) && $data['member_id'] = MEMBER_ID;
        
//      $data['content'] = htmlspecialchars_decode($data['content']);

        $result = $this->modelCourse->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '课堂' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '课堂操作成功', $url] : [RESULT_ERROR, $this->modelCourse->getError()];
    }

    /**
     * 获取课堂信息
     */
    public function getArticleInfo($where = [], $field = 'a.*,m.nickname')
    {
        
        $this->modelCourse->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelCourse->join = $join;
        
        return $this->modelCourse->getInfo($where, $field);
    }
    

    
    /**
     * 课堂删除
     */
    public function articleDel($where = [])
    {
        
        $result = $this->modelCourse->deleteInfo($where);
        
        $result && action_log('删除', '课堂删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '课堂删除成功'] : [RESULT_ERROR, $this->modelCourse->getError()];
    }

    /**
     * 课堂删除
     */
    public function update_info($where = [])
    {

        //dd($where);

        $result = $this->modelCourse->setInfo($where);


        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }
}
